/**------File Info----------------------------------------------
** File name:               Frame_io.c
** Latest modified date:    2024-9-25
** Latest version:          V1.0.0
** Description:             GPIO control
** 
**------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Description:
**
*************************************************************/
#include "Frame.h"
/*************************************************************
** Function name:       Set_IO_High
** Descriptions:        设置IO口电平高
** Input parameters:    gIO:SIOControl_Struct结构体指�?
** Output parameters:   None
** Returned value:      None
** Remarks:             None
*************************************************************/
void Set_IO_High(PIOControl_Struct gio)
{
    gio->state = IO_Stage_HIGH;
    gio->onoff = IO_Stage_HIGH;
    gio->SetIOLeave(IO_Stage_HIGH);
}
/*************************************************************
** Function name:       Set_IO_Low
** Descriptions:        设置IO口电平低
** Input parameters:    gIO:SIOControl_Struct结构体指�?
** Output parameters:   None
** Returned value:      None
** Remarks:             None
*************************************************************/
void Set_IO_Low(PIOControl_Struct gio)
{
    gio->state = IO_Stage_LOW;
    gio->onoff = IO_Stage_LOW;
    gio->SetIOLeave(IO_Stage_LOW);
}
/*************************************************************
** Function name:       IOToggle
** Descriptions:        翻转IO电平
** Input parameters:    gIO:SIOControl_Struct结构体指�?
** Output parameters:   None
** Returned value:      None
** Remarks:             None
*************************************************************/
void IOToggle(PIOControl_Struct gio)
{
    gio->onoff = ~gio->onoff;
    gio->SetIOLeave(gio->onoff);
}
/*************************************************************
** Function name:       SetIOMode
** Descriptions:        设置IO口模�?
** Input parameters:    gIO:SIOControl_Struct结构体指�?
                        xmode:IO模式：高，低，方波，高一段时间低，低一段时间高
                        xtim：方波时间或保持时间，单位S
** Output parameters:   None
** Returned value:      None
** Remarks:             None
*************************************************************/
void SetIOMode(PIOControl_Struct gio,IO_Stage xmode,float xtim)
{
    switch (xmode)
    {
    case IO_Stage_HIGH:
        Set_IO_High(gio);
        break;
    case IO_Stage_LOW:
        Set_IO_Low(gio);
        break;
    case IO_Stage_Toggle:
        gio->state = IO_Stage_Toggle;
        gio->startTime = FRAME_GETTIME();
        gio->cycle = xtim;
        break;
    case IO_Stage_HighToLow:
        gio->state = IO_Stage_HighToLow;
        gio->startTime = FRAME_GETTIME();
        Set_IO_High(gio);
        gio->keep = xtim;
        break;
    case IO_Stage_LowToHigh:
        gio->state = IO_Stage_LowToHigh;
        gio->startTime = FRAME_GETTIME();
        Set_IO_Low(gio);
        gio->keep = xtim;
        break;
    default:
        break;
    }
}
/*************************************************************
** Function name:       IORunCycle
** Descriptions:        循环函数
** Input parameters:    None
** Output parameters:   None
** Returned value:      None
** Remarks:             None
*************************************************************/
void IORunCycle(PIOControl_Struct gio)
{
    switch (gio->state)
    {
    case IO_Stage_Toggle:
        if(FRAME_TIMEOUT(gio->cycle * 1000,gio->startTime))
        {
        gio->startTime = FRAME_GETTIME();
        IOToggle(gio);
    	}
        break;
    case IO_Stage_HighToLow:
        if(FRAME_TIMEOUT(gio->cycle * 1000,gio->startTime))
        {
            Set_IO_Low(gio);
            gio->state = IO_Stage_LOW;
        }
        break;
    case IO_Stage_LowToHigh:
        if(FRAME_TIMEOUT(gio->cycle * 1000,gio->startTime))
        {
            Set_IO_High(gio);
            gio->state = IO_Stage_HIGH;
        }
        break;
    default:
        break;
    }
}